import asyncio, aiohttp

#协程的原理
#通过与调用方协作，产出调用方提供的值

#协程程序
def n():
    yield 'great!'
def j():
    p = n()
    for i in range(10):
        try:
            result = yield next(p)

        except:
            result = yield 0
        print(result)
l=j()
for i in range(3):
    print(next(l))

#实现异步程序
async def q():
    print('你好棒！')
loop = asyncio.get_event_loop()
result = [q() for i in range(5)]
loop.run_until_complete(asyncio.wait(result))
#loop.close()

#异步网站请求
k=["https://www.vip.com/"]*3
async def requests_weipinhui(url):
    async with aiohttp.ClientSession() as session:   
        async with session.get(url) as response:
            result_1 = await response.text()
            return result_1
loop1 = asyncio.get_event_loop()
task = [requests_weipinhui(url) for url in k]
#task = []
#for url in k:
#    task.append(asyncio.ensure_future(requests_weipinhui(url)))
m = loop1.run_until_complete(asyncio.gather(*task))
loop1.close()
print(f'{m}')

